package com.ygqh.baby.service.impl;

import com.alibaba.fastjson.JSON;
import com.ygqh.baby.service.SmsService;
import com.ygqh.baby.service.YgMobileService;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class SmsServiceImpl implements SmsService {

    private Logger logger = Logger.getLogger(SmsService.class);
    @Autowired
    private YgMobileService ygMobileService;
    @Value("${sms.url}")
    private String url;
    @Value("${sms.userId}")
    private String userId;
    @Value("${sms.password}")
    private String password;
    @Value("${sms.marketing.userId}")
    private String marketingUserId;
    @Value("${sms.marketing.password}")
    private String marketingPassword;
    @Value("${sms.iMobiCount}")
    private String iMobiCount;
    @Value("${sms.pszSubPort}")
    private String pszSubPort;
    @Value("${sms.MsgId}")
    private String MsgId;
    @Value("${sms.isSend}")
    private Boolean isSend;

    @Override
    public void sendSms(String phone, String content) {
        sendSms(phone, content, false);
    }

    @Override
    public void sendSms(String phone, String content, boolean vCode) {
        ygMobileService.add(phone, content);
        if (isSend) {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost httppost = new HttpPost(url);
            List<BasicNameValuePair> formparams = new ArrayList<>();

            if (vCode) {
                formparams.add(new BasicNameValuePair("userId", userId));
                formparams.add(new BasicNameValuePair("password", password));
            } else {
                formparams.add(new BasicNameValuePair("userId", marketingUserId));
                formparams.add(new BasicNameValuePair("password", marketingPassword));
            }
            formparams.add(new BasicNameValuePair("pszMobis", phone));
            formparams.add(new BasicNameValuePair("pszMsg", content));
            formparams.add(new BasicNameValuePair("iMobiCount", iMobiCount));
            formparams.add(new BasicNameValuePair("pszSubPort", pszSubPort));
            formparams.add(new BasicNameValuePair("MsgId", MsgId));
            UrlEncodedFormEntity uefEntity;

            System.out.println("======================================");
            System.out.println(JSON.toJSONString(formparams));
            System.out.println("======================================");
            try {
                uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
                httppost.setEntity(uefEntity);
                logger.debug("executing request " + httppost.getURI());
                logger.debug("content:" + content);
                CloseableHttpResponse response = httpclient.execute(httppost);
                try {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        System.out.println("--------------------------------------");
                        System.out.println("Response content: " + IOUtils.toString(entity.getContent(), "UTF-8"));
                        System.out.println("--------------------------------------");
                    }
                } finally {
                    response.close();
                }
            } catch (ParseException | IOException e) {
                logger.error("短信息发送失败 ：手机号： " + phone + "内容：" + content);
                e.printStackTrace();
            } finally {
                // 关闭连接,释放资源
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
